<template>
  <transition name="gallery-fade">
    <div v-if="visible" class="gallery-overlay" @click="handleOverlayClick">
      <div class="gallery-container">
        <!-- 关闭按钮 -->
        <button class="close-btn" @click="handleClose" aria-label="关闭">
          <X :size="24" />
        </button>
        
        <!-- 图片索引 -->
        <div class="image-counter">
          {{ currentIndex + 1 }} / {{ images.length }}
        </div>
        
        <!-- 主图片容器 -->
        <div class="image-container" ref="imageContainer">
          <div 
            class="image-track"
            :style="{ transform: `translateX(${-currentIndex * 100}%)` }"
            @touchstart="handleTouchStart"
            @touchmove="handleTouchMove"
            @touchend="handleTouchEnd"
          >
            <div 
              v-for="(image, index) in images" 
              :key="index"
              class="image-slide"
            >
              <img 
                :src="image.src" 
                :alt="image.alt || ''"
                class="gallery-image"
                @click.stop=""
              />
            </div>
          </div>
        </div>
        
        <!-- 导航按钮 -->
        <button 
          v-if="images.length > 1"
          class="nav-btn prev-btn" 
          @click="previousImage"
          :disabled="currentIndex === 0"
        >
          <ChevronLeft :size="24" />
        </button>
        
        <button 
          v-if="images.length > 1"
          class="nav-btn next-btn" 
          @click="nextImage"
          :disabled="currentIndex === images.length - 1"
        >
          <ChevronRight :size="24" />
        </button>
        
        <!-- 缩略图导航 -->
        <div v-if="images.length > 1" class="thumbnails">
          <div 
            v-for="(image, index) in images" 
            :key="index"
            :class="['thumbnail', { active: index === currentIndex }]"
            @click="goToImage(index)"
          >
            <img :src="image.src" :alt="image.alt || ''" />
          </div>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
import { X, ChevronLeft, ChevronRight } from 'lucide-vue-next'

export default {
  name: 'ImageGallery',
  components: {
    X,
    ChevronLeft,
    ChevronRight
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    images: {
      type: Array,
      default: () => []
    },
    initialIndex: {
      type: Number,
      default: 0
    }
  },
  emits: ['close', 'change'],
  data() {
    return {
      currentIndex: 0,
      touchStartX: 0,
      touchStartTime: 0,
      isAnimating: false
    }
  },
  watch: {
    visible(newVal) {
      if (newVal) {
        this.currentIndex = this.initialIndex
        document.body.style.overflow = 'hidden'
      } else {
        document.body.style.overflow = ''
      }
    },
    initialIndex(newVal) {
      this.currentIndex = newVal
    }
  },
  methods: {
    handleClose() {
      this.$emit('close')
    },
    
    handleOverlayClick(e) {
      if (e.target === e.currentTarget) {
        this.handleClose()
      }
    },
    
    nextImage() {
      if (this.currentIndex < this.images.length - 1) {
        this.currentIndex++
        this.$emit('change', this.currentIndex)
      }
    },
    
    previousImage() {
      if (this.currentIndex > 0) {
        this.currentIndex--
        this.$emit('change', this.currentIndex)
      }
    },
    
    goToImage(index) {
      this.currentIndex = index
      this.$emit('change', index)
    },
    
    handleTouchStart(e) {
      this.touchStartX = e.touches[0].clientX
      this.touchStartTime = Date.now()
    },
    
    handleTouchMove(e) {
      if (!this.touchStartX) return
      e.preventDefault()
    },
    
    handleTouchEnd(e) {
      if (!this.touchStartX) return
      
      const currentX = e.changedTouches[0].clientX
      const deltaX = currentX - this.touchStartX
      const deltaTime = Date.now() - this.touchStartTime
      const velocity = Math.abs(deltaX) / deltaTime
      
      const threshold = 50
      const fastSwipe = velocity > 0.3 && Math.abs(deltaX) > 30
      
      if (Math.abs(deltaX) > threshold || fastSwipe) {
        if (deltaX > 0) {
          this.previousImage()
        } else {
          this.nextImage()
        }
      }
      
      this.touchStartX = 0
      this.touchStartTime = 0
    }
  },
  
  beforeUnmount() {
    document.body.style.overflow = ''
  }
}
</script>

<style scoped>
.gallery-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.95);
  backdrop-filter: blur(10px);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.gallery-container {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
  box-sizing: border-box;
}

.close-btn {
  position: absolute;
  top: 20px;
  right: 20px;
  width: 44px;
  height: 44px;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  cursor: pointer;
  transition: all 0.3s ease;
  z-index: 10;
}

.close-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: scale(1.1);
}

.image-counter {
  position: absolute;
  top: 20px;
  left: 20px;
  color: white;
  font-size: 14px;
  background: rgba(0, 0, 0, 0.5);
  padding: 8px 12px;
  border-radius: 20px;
  z-index: 10;
}

.image-container {
  flex: 1;
  width: 100%;
  max-width: 90vw;
  max-height: 70vh;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.image-track {
  display: flex;
  width: 100%;
  height: 100%;
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.image-slide {
  flex: 0 0 100%;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.gallery-image {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

.nav-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 50px;
  height: 50px;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  cursor: pointer;
  transition: all 0.3s ease;
  z-index: 10;
}

.nav-btn:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-50%) scale(1.1);
}

.nav-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.prev-btn {
  left: 20px;
}

.next-btn {
  right: 20px;
}

.thumbnails {
  display: flex;
  gap: 8px;
  margin-top: 20px;
  padding: 0 20px;
  max-width: 100%;
  overflow-x: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.thumbnails::-webkit-scrollbar {
  display: none;
}

.thumbnail {
  flex-shrink: 0;
  width: 60px;
  height: 60px;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  border: 2px solid transparent;
  transition: all 0.3s ease;
}

.thumbnail.active {
  border-color: #FF6B35;
  box-shadow: 0 0 12px rgba(255, 107, 53, 0.5);
}

.thumbnail img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.thumbnail:hover {
  transform: scale(1.1);
}

/* 过渡动画 */
.gallery-fade-enter-active,
.gallery-fade-leave-active {
  transition: all 0.3s ease;
}

.gallery-fade-enter-from,
.gallery-fade-leave-to {
  opacity: 0;
  transform: scale(0.9);
}

/* 移动端优化 */
@media (max-width: 768px) {
  .gallery-container {
    padding: 10px;
  }
  
  .close-btn {
    top: 10px;
    right: 10px;
    width: 40px;
    height: 40px;
  }
  
  .image-counter {
    top: 10px;
    left: 10px;
    font-size: 12px;
    padding: 6px 10px;
  }
  
  .nav-btn {
    width: 40px;
    height: 40px;
  }
  
  .prev-btn {
    left: 10px;
  }
  
  .next-btn {
    right: 10px;
  }
  
  .thumbnails {
    margin-top: 10px;
    padding: 0 10px;
  }
  
  .thumbnail {
    width: 50px;
    height: 50px;
  }
}
</style>